<?php
namespace Admin\Controller;
use Think\Controller;
class ArticleController extends CommonController
{
    var $article;

    function __construct()
    {
        parent::__construct();
        $this->article = M('article');

    }
        function index()
        {
            $Article=D('article');
            $articles = $Article->relation(true)->select();
            //dump($articles);exit;
            $this->assign('articles', $articles);
            $this->display();
        }

    function edit(){
        $Category=D('category');
        $categories=$Category->where("parent_id=0")->order('sort_order')->select();
        //echo $this->category->getLastSql();exit;
        foreach ($categories as &$v){
            $id=$v["id"];
            $children=$Category->where("parent_id=$id")->select();
            //dump($children);exit;
            $v["children"]=$children;
        }
        //dump($categories);exit;

        $this->assign('categories',$categories);
        //dump($_POST);exit;
        $id=I("get.id");

        $a=$this->article->find($id);
        //echo  $this->article->getLastSql();exit;
        $this->assign("a",$a);

        $files=explode("|",$a['file']);
        //dump($a);exit;
        $this->assign("files",$files);
        //dump($a);exit;
        //dump($_POST);exit;
        $this->display();
        
    }
    function add(){
       $Category=D('category');
        $categories=$Category->where("parent_id=0")->order('sort_order')->select();
        //echo $this->category->getLastSql();exit;
        foreach ($categories as &$v){
            $id=$v["id"];
            $children=$Category->where("parent_id=$id")->select();
            //dump($children);exit;
            $v["children"]=$children;
        }
        //dump($categories);exit;

        $this->assign('categories',$categories);
        $this->display();
    }
    function store(){
        //dump($_POST);exit;
        $this->article->create();

        $this->article->file=implode("|",I("post.file"));

        $this->article->date=strtotime(I("post.date"));
        $this->article->add();
        $this->redirect('index');
    }
    function update()
    {

        //dump($_POST);exit;
        $this->article->create();
        $this->article->save();

        $this->success('修改成功',U('index'));
    }

}